<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="任务名称" prop="requirementName">
        <el-input
          v-model="queryParams.requirementName"
          placeholder="请输入任务名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务描述" prop="requirementDesc">
        <el-input
          v-model="queryParams.requirementDesc"
          placeholder="请输入任务描述"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务类型" prop="requirementType">
        <el-select v-model="queryParams.requirementType" placeholder="请选择任务类型" clearable size="small">
          <el-option
            v-for="dict in requirementTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="任务类别" prop="requirementKind">
        <el-select v-model="queryParams.requirementKind" placeholder="请选择任务类别" clearable size="small">
          <el-option
            v-for="dict in requirementKindOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="重要程度" prop="requirementLevel">
        <el-select v-model="queryParams.requirementLevel" placeholder="请选择重要程度" clearable size="small">
          <el-option
            v-for="dict in requirementLevelOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="涉及客户" prop="customers">
        <el-input
          v-model="queryParams.customers"
          placeholder="请输入涉及客户"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="期望上线" prop="expectTime">
        <el-date-picker clearable size="small" style="width: 200px"
                        v-model="queryParams.expectTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择期望上线">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="任务来源" prop="requirementSource">
        <el-input
          v-model="queryParams.requirementSource"
          placeholder="请输入任务来源"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="TS单号" prop="tsTid">
        <el-input
          v-model="queryParams.tsTid"
          placeholder="请输入TS单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="TS版本" prop="tsVersion">
        <el-select v-model="queryParams.tsVersion" placeholder="请选择TS版本" clearable size="small">
          <el-option
            v-for="dict in tsVersionOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="开发人" prop="devStaff">
        <el-input
          v-model="queryParams.devStaff"
          placeholder="请输入开发人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="测试人" prop="testStaff">
        <el-input
          v-model="queryParams.testStaff"
          placeholder="请输入测试人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mtool:requirement:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mtool:requirement:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mtool:requirement:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['mtool:requirement:export']"
        >导出
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="requirementList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--      <el-table-column label="任务id" align="center" prop="requirementId" />-->
      <el-table-column label="任务名称" align="center" prop="requirementName"/>
      <el-table-column label="任务描述" align="center" prop="requirementDesc"/>
      <el-table-column label="任务类型" align="center" prop="requirementType" :formatter="requirementTypeFormat"/>
      <el-table-column label="任务类别" align="center" prop="requirementKind" :formatter="requirementKindFormat"/>
      <el-table-column label="重要程度" align="center" prop="requirementLevel" :formatter="requirementLevelFormat"/>
      <el-table-column label="涉及客户" align="center" prop="customers"/>
      <el-table-column label="期望上线" align="center" prop="expectTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expectTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="任务来源" align="center" prop="requirementSource"/>
      <el-table-column label="TS单号" align="center" prop="tsTid"/>
      <el-table-column label="TS版本" align="center" prop="tsVersion" :formatter="tsVersionFormat"/>
      <el-table-column label="开发人" align="center" prop="devStaff"/>
      <el-table-column label="测试人" align="center" prop="testStaff"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mtool:requirement:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mtool:requirement:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改任务记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="任务名称" prop="requirementName">
              <el-input v-model="form.requirementName" placeholder="请输入任务名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="重要程度">
              <el-select v-model="form.requirementLevel" placeholder="请选择重要程度">
                <el-option
                  v-for="dict in requirementLevelOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="任务描述" prop="requirementDesc">
              <el-input v-model="form.requirementDesc" type="textarea" placeholder="请输入任务描述"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="任务类型">
              <el-select v-model="form.requirementType" placeholder="请选择任务类型">
                <el-option
                  v-for="dict in requirementTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="任务类别">
              <el-select v-model="form.requirementKind" placeholder="请选择任务类别">
                <el-option
                  v-for="dict in requirementKindOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="涉及客户" prop="customers">
              <el-input v-model="form.customers" placeholder="请输入涉及客户"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="期望上线" prop="expectTime">
              <el-date-picker clearable size="small" style="width: 200px"
                              v-model="form.expectTime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="选择期望上线">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="任务来源" prop="requirementSource">
              <el-input v-model="form.requirementSource" placeholder="请输入任务来源"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="TS单号" prop="tsTid">
              <el-input v-model="form.tsTid" placeholder="请输入TS单号"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="TS版本">
              <el-select v-model="form.tsVersion" placeholder="请选择TS版本">
                <el-option
                  v-for="dict in tsVersionOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开发工作量预估" prop="devPlanWorkload">
              <el-input v-model="form.devPlanWorkload" placeholder="请输入开发工作量预估"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开发计划完成时间" prop="devPlanTime">
              <el-date-picker clearable size="small" style="width: 200px"
                              v-model="form.devPlanTime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="选择开发计划完成时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开发实际完成时间" prop="devFinishTime">
              <el-date-picker clearable size="small" style="width: 200px"
                              v-model="form.devFinishTime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="选择开发实际完成时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开发人" prop="devStaff">
              <el-input v-model="form.devStaff" placeholder="请输入开发人"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="测试人" prop="testStaff">
              <el-input v-model="form.testStaff" placeholder="请输入测试人"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="UAT验证" prop="uatCheck">
              <el-input v-model="form.uatCheck" placeholder="请输入UAT验证"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="PRD验证" prop="prdCheck">
              <el-input v-model="form.prdCheck" placeholder="请输入PRD验证"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {listRequirement, getRequirement, delRequirement, addRequirement, updateRequirement, exportRequirement} from "@/api/mtool/requirement";

  export default {
    name: "Requirement",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 任务记录表格数据
        requirementList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 任务类型字典
        requirementTypeOptions: [],
        // 任务类别字典
        requirementKindOptions: [],
        // 重要程度字典
        requirementLevelOptions: [],
        // TS版本字典
        tsVersionOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          requirementName: undefined,
          requirementDesc: undefined,
          requirementType: undefined,
          requirementKind: undefined,
          requirementLevel: undefined,
          customers: undefined,
          expectTime: undefined,
          requirementSource: undefined,
          tsTid: undefined,
          tsVersion: undefined,
          devStaff: undefined,
          testStaff: undefined,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    created() {
      this.getList();
      this.getDicts("mtool_requirement_type").then(response => {
        this.requirementTypeOptions = response.data;
      });
      this.getDicts("mtool_requirement_kind").then(response => {
        this.requirementKindOptions = response.data;
      });
      this.getDicts("mtool_requirement_level").then(response => {
        this.requirementLevelOptions = response.data;
      });
      this.getDicts("mtool_ts_version").then(response => {
        this.tsVersionOptions = response.data;
      });
    },
    methods: {
      /** 查询任务记录列表 */
      getList() {
        this.loading = true;
        listRequirement(this.queryParams).then(response => {
          this.requirementList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 任务类型字典翻译
      requirementTypeFormat(row, column) {
        return this.selectDictLabel(this.requirementTypeOptions, row.requirementType);
      },
      // 任务类别字典翻译
      requirementKindFormat(row, column) {
        return this.selectDictLabel(this.requirementKindOptions, row.requirementKind);
      },
      // 重要程度字典翻译
      requirementLevelFormat(row, column) {
        return this.selectDictLabel(this.requirementLevelOptions, row.requirementLevel);
      },
      // TS版本字典翻译
      tsVersionFormat(row, column) {
        return this.selectDictLabel(this.tsVersionOptions, row.tsVersion);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          requirementId: undefined,
          requirementName: undefined,
          requirementDesc: undefined,
          requirementType: undefined,
          requirementKind: undefined,
          requirementLevel: undefined,
          customers: undefined,
          expectTime: undefined,
          requirementSource: undefined,
          tsTid: undefined,
          tsVersion: undefined,
          devPlanWorkload: undefined,
          devPlanTime: undefined,
          devFinishTime: undefined,
          devStaff: undefined,
          testStaff: undefined,
          uatCheck: undefined,
          prdCheck: undefined,
          remark: undefined,
          status: "0",
          delFlag: undefined,
          createBy: undefined,
          createTime: undefined,
          updateBy: undefined,
          updateTime: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.requirementId)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加任务记录";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const requirementId = row.requirementId || this.ids
        getRequirement(requirementId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改任务记录";
        });
      },
      /** 提交按钮 */
      submitForm: function () {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.requirementId != undefined) {
              updateRequirement(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            } else {
              addRequirement(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const requirementIds = row.requirementId || this.ids;
        this.$confirm('是否确认删除任务记录编号为"' + requirementIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delRequirement(requirementIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function () {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有任务记录数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return exportRequirement(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function () {
        });
      }
    }
  };
</script>
